<!DOCTYPE HTML>

<html>
<head>
<title>WebGL 2.0 Test: BlitFramebuffer Result Must Be Displayed</title>
<style type="text/css">
.nomargin {
  margin: 0px auto;
}
</style>

<script>
function sendResult(status, detail) {
  console.log(detail);
  if (window.domAutomationController) {
    window.domAutomationController.send(status);
  } else {
    console.log(status);
  }
}

var gl;
var fbo;
var width;
var height;
var numFramesBeforeBlit = 15;
var numFramesBeforeEnd = 15;

function main() {
  var canvas = document.getElementById("c");
  gl = canvas.getContext('webgl2', {alpha: false, antialias: false,
                                    depth: false, stencil: false});
  if (!gl) {
    sendResult("FAILURE", "WebGL 2.0 context not supported");
    return;
  }

  // Clear background of canvas to red
  gl.clearColor(1.0, 0.0, 0.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Wait a few frames and then clearBufferfv to green, to ensure
  // canvas was composited
  window.requestAnimationFrame(maybeClearBufferfv);
}

function maybeClearBufferfv()
{
  if (--numFramesBeforeBlit == 0) {
    gl.clearBufferfv(gl.COLOR, 0, [ 0.0, 1.0, 0.0, 1.0 ]);
    // That's the only operation done to the canvas. Wait to signal
    // acknowledgment to the harness.
    window.requestAnimationFrame(waitForFinish);
  } else {
    // Do this again
    window.requestAnimationFrame(maybeClearBufferfv);
  }
}

function waitForFinish()
{
  if (--numFramesBeforeEnd == 0) {
    sendResult("SUCCESS", "Test complete");
  } else {
    window.requestAnimationFrame(waitForFinish);
  }
}
</script>
</head>
<body onload="main()">
<canvas id="c" width="200" height="200" class="nomargin" style="position:absolute; top:0px; left:0px"></canvas>
</div>
</body>
</html>
